package com.supply.dl.utils;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * Description: AlertDialog操作类
 * Author：skyTian
 * Time:2017/11/14 10:36
 * CopyRight:com,sky
 */
public class DialogUtils {

    /**
     * 创建弹出框
     *
     * @param title
     * @param showData
     * @param onClickListener
     * @return
     */
    public static AlertDialog getAlertDialog(Context context, String title, String[] showData, final OnDialogItemClickListener onClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);

        final int[] currentIndex = {0};

        builder.setSingleChoiceItems(showData, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                currentIndex[0] = i;
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                onClickListener.onItemClickListener(currentIndex[0]);
                dialogInterface.dismiss();
            }
        });
        return builder.create();
    }

    /**
     * 生产一个带有2个 按钮的弹出框
     *
     * @param context
     * @param title
     * @param content
     * @param leftBtn
     * @param rightBtn
     * @param onleftClickListener
     * @param onRightClickListener
     * @return
     */
    public static AlertDialog showDialog(Context context, String title, String content, String leftBtn, String rightBtn,
                                         DialogInterface.OnClickListener onleftClickListener, DialogInterface.OnClickListener onRightClickListener) {
        AlertDialog noticeDialog = new AlertDialog.Builder(context).setTitle(title)
                .setMessage(content)
                .setNegativeButton(leftBtn, onleftClickListener)
                .setPositiveButton(rightBtn, onRightClickListener).create();

        return noticeDialog;
    }


    /**
     * 得到一个提示框
     *
     * @param context
     * @param title
     * @param content
     * @param onClickListener
     * @return
     */
    public static AlertDialog showDialog(Context context, String title, String content, DialogInterface.OnClickListener onClickListener) {
        AlertDialog noticeDialog = new AlertDialog.Builder(context).setTitle(title)
                .setMessage(content)
                .setPositiveButton("确定", onClickListener).create();

        return noticeDialog;
    }


    /**
     * 得到一个提示框
     *
     * @param context
     * @param content
     * @param onClickListener
     * @return
     */
    public static AlertDialog showDialogNoTitle(Context context, String content, DialogInterface.OnClickListener onClickListener) {
        Log.i("Ok", context.getClass().getSimpleName());
        AlertDialog noticeDialog = new AlertDialog.Builder(context)
                .setMessage(content)
                .setPositiveButton("确定", onClickListener).create();
        return noticeDialog;
    }


    /**
     * 用于AlertDialog 显示数据并且 回传,单选监听
     */
    public interface OnDialogItemClickListener {
        void onItemClickListener(int currentIndexs);
    }

    /**
     * 多选dialog，数据回调接口
     */
    public interface OnDialogItemsClickListener {
        void onItemsClickListener(ArrayList<Integer> indexs);
    }


    /**
     * 选择时间
     *
     * @param tv
     */
    public static void setIime(final Context context, final TextView tv) {
        //先选择日期，后选择时间
        final Calendar currentTime = Calendar.getInstance();
        final DatePickerDialog[] datePickerDialogs = new DatePickerDialog[1];

        datePickerDialogs[0] = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                currentTime.set(year, month, day);//将点击获得的年月日获取到calendar中。
                datePickerDialogs[0].dismiss();

                final TimePickerDialog[] timePickerDialogs = new TimePickerDialog[1];
                timePickerDialogs[0] = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int hour, int minite) {
                        currentTime.set(Calendar.HOUR_OF_DAY, hour);//设置时间的另一种方式
                        currentTime.set(Calendar.MINUTE, minite);
                        tv.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(currentTime.getTime()));
                        timePickerDialogs[0].dismiss();
                    }
                }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE), true);
                timePickerDialogs[0].show();
            }
        }, currentTime.get(Calendar.YEAR), currentTime.get(Calendar.MONTH), currentTime.get(Calendar.DAY_OF_MONTH));
        datePickerDialogs[0].show();
    }


    /**
     * 选择时间，并回调callback
     *
     * @param context
     * @param tv
     * @param onTimeBack
     */
    public static void setTimeWithCallback(final Context context, final TextView tv, final OnTimeBack onTimeBack) {
        //先选择日期，后选择时间
        final Calendar currentTime = Calendar.getInstance();
        final DatePickerDialog[] datePickerDialogs = new DatePickerDialog[1];

        datePickerDialogs[0] = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                currentTime.set(year, month, day);//将点击获得的年月日获取到calendar中。
                datePickerDialogs[0].dismiss();

                final TimePickerDialog[] timePickerDialogs = new TimePickerDialog[1];
                timePickerDialogs[0] = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int hour, int minite) {
                        currentTime.set(Calendar.HOUR_OF_DAY, hour);//设置时间的另一种方式
                        currentTime.set(Calendar.MINUTE, minite);
                        tv.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(currentTime.getTime()));

                        if (onTimeBack != null) {
                            onTimeBack.onTimeBack(tv.getText().toString());
                        }
                        timePickerDialogs[0].dismiss();
                    }
                }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE), true);
                timePickerDialogs[0].show();
            }
        }, currentTime.get(Calendar.YEAR), currentTime.get(Calendar.MONTH), currentTime.get(Calendar.DAY_OF_MONTH));
        datePickerDialogs[0].show();
    }


    /**
     * 选择时间过后，返回
     */
    public interface OnTimeBack {
        void onTimeBack(String selectTime);
    }
}
